package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(StrategyComment strategyComment) {
       strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());

       strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).get();
    }

    @Override
    public List<StrategyComment> list() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> queryByStrategyId(StrategyCommentQuery qo) {
        //查询对象
        Query query = new Query();
        if(StringUtils.hasLength(qo.getStrategyId())){
         query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        Long totalCount = mongoTemplate.count(query,StrategyComment.class);
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"createTime");
        query.with(pageable);
        List<StrategyComment> strategyComments = mongoTemplate.find(query, StrategyComment.class);
        return new PageImpl(strategyComments,pageable,totalCount);
    }

    @Override
    public void commentThumb(String cid, String sid) {
        //通过用户评论id查询当前评论对象
        StrategyComment strategyComment = this.get(cid);

        //在从评论对象中获取点赞集合
        List<String> thumbuplist = strategyComment.getThumbuplist();

        //查询点赞集合中是否包含当前用户id
        if(thumbuplist.contains(sid)){
            //如果有，则说明点赞过，将评论点赞集合中用户id移除,点赞数-1
            thumbuplist.remove(sid);
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
        }else{
            //如果没有，则说明为点赞过，将评论点赞集合中添加用户id，点赞数+1
            thumbuplist.add(sid);
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
        }

         //更新数据
         this.update(strategyComment);

    }


    //通过评论id查询当前评论对象



}
